package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "购物车相关接口")
@Slf4j
@RequestMapping("/user/shoppingCart") //映射路径抽取
@RestController  // 标识是请求处理类, 可以为bean起别名
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     *
     * @param dto
     * @return
     */
    @ApiOperation("添加购物车")
    @PostMapping("/add")
    public Result addShoppingCart(@RequestBody ShoppingCartDTO dto) {
        //1.输出日志
        log.info("添加购物车:dto = {}", dto);
        //2.调用service, 进行存储数据
        shoppingCartService.addShoppingCart(dto);
        //3.响应结果
        return Result.success();
    }

    /**
     * 查询购物车
     *
     * @return
     */

    @ApiOperation("查询购物车")
    @GetMapping("/list")
    public Result list() {
        //1.输出日志
        log.info("查询购物车");
        //2.调用service层,获取数据
        List<ShoppingCart> shoppingCartList = shoppingCartService.list();
        //3.响应结果
        return Result.success(shoppingCartList);
    }

    /**
     * 清空购物车
     *
     * @return
     */

    @ApiOperation("清空购物车")
    @DeleteMapping("/clean")
    public Result clean() {
        //1.输出日志
        log.info("清空购物车");
        //2.调用service, 记性删除操作
        shoppingCartService.clean();
        //3.响应结果
        return Result.success();
    }

    /**
     * 删除购物车中的一个商品
     * @param dto
     * @return
     */
    @ApiOperation("删除购物车中的一个商品")
    @PostMapping("/sub")
    public Result subShoppingCart(@RequestBody ShoppingCartDTO dto) {
        //1.输出日志
        log.info("删除购物车中的一个商品:dto = {}", dto);
        //2.调用service层
        shoppingCartService.subShoppingCart(dto);
        //3.响应结果
        return Result.success();
    }
}
